package com.ikkong.biz.controller;

import java.util.*;

import com.google.common.collect.Sets;
import com.ikkong.biz.meta.intercept.ClassesIntercept;
import com.ikkong.biz.service.TeacherService;
import com.ikkong.biz.service.impl.TeacherServiceImpl;
import com.ikkong.core.base.BaseController;
import com.ikkong.core.constant.ConstCache;
import com.ikkong.core.dao.Db;
import com.ikkong.core.exception.BizException;
import com.ikkong.core.jfinal.ext.kit.JStrKit;
import com.ikkong.core.jfinal.ext.kit.JsonKit;
import com.ikkong.core.toolbox.Conver;
import com.ikkong.core.toolbox.Func;
import com.ikkong.core.toolbox.Record;
import com.ikkong.qy.util.WeixinApi;
import com.ikkong.system.controller.base.AdminBaseController;
import com.ikkong.biz.model.Classes;
import com.ikkong.biz.model.Student;
import com.ikkong.biz.service.ClassesService;
import com.ikkong.biz.service.impl.ClassesServiceImpl;
import com.jfinal.aop.Before;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.ehcache.CacheKit;
import com.jfinal.upload.UploadFile;

import org.apache.commons.lang3.StringUtils;
import org.beetl.sql.ext.jfinal.Trans;

import static com.ikkong.common.config.MainConfig.map;

/**
 * Generated by Blade.
 * 2016-11-13 16:56:20
 */
public class ClassesController extends AdminBaseController {
	private static String CODE = "classes";
	private static String PERFIX = "tb_classes";
	private static String LIST_SOURCE = "Classes.list";
	private static String BASE_PATH = "/biz/classes/";
    private static String GRADUATE_LIST_SOURCE = "Classes.graduate_list";
    private static String GRADUATE_DETAIL_LIST_SOURCE = "Classes.graduate_detail_list";

	ClassesService service = new ClassesServiceImpl();

	public void index() {
		setAttr("code", CODE);
		render(BASE_PATH + "classes.html");
	}

	public void graduate() {
		setAttr("code", CODE);
		render(BASE_PATH + "graduate_classes.html");
	}

	public void graduate_detail() {
        setAttr("classid",getPara(0));
		setAttr("code", CODE);
		render(BASE_PATH + "graduate_detail.html");
	}

	public void add() {
		setAttr("code", CODE);
		render(BASE_PATH + "classes_add.html");
	}

	public void edit() {
		String id = getPara(0);
		Classes classes = service.findById(id);
		setAttr("model", JsonKit.toJson(classes));
		setAttr("id", id);
		setAttr("code", CODE);
		render(BASE_PATH + "classes_edit.html");
	}

	public void view() {
		String id = getPara(0);
		Classes classes = service.findById(id);
        Record maps = service.convert(classes);

		setAttr("model", JsonKit.toJson(maps));
		setAttr("id", id);
		setAttr("code", CODE);
		render(BASE_PATH + "classes_view.html");
	}

	public void list() {
		Object grid = paginate(LIST_SOURCE,new ClassesIntercept());
		renderJson(grid);
	}

	public void graduate_list() {
		Object grid = paginate(GRADUATE_LIST_SOURCE,new ClassesIntercept());
		renderJson(grid);
	}

	public void graduate_detail_list() {
		Object grid = paginate(GRADUATE_DETAIL_LIST_SOURCE);
		renderJson(grid);
	}

	public void save() {
		Classes classes = mapping(PERFIX, Classes.class);
        String teachers = classes.getTeachers();
        if (StrKit.notBlank(teachers)){
            Set<String> set = Sets.newHashSet();
            String[] ss = teachers.split(",");
            set.addAll(Arrays.asList(ss));
            String dest = JStrKit.wrapWithDelimiter(StringUtils.join(set,','));
            classes.setTeachers(dest);
        }
		boolean temp = service.saveClass(classes);

		if (temp) {
			renderJson(success(SAVE_SUCCESS_MSG));
		} else {
			renderJson(error(SAVE_FAIL_MSG));
		}
	}

	public void update() {
		Classes classes = mapping(PERFIX, Classes.class);
		classes.setVersion(classes.getVersion()+1);
		int count = Db.init().queryInt("select count(1) from tb_classes t where t.grade_id=#{grade_id} and t.class_name=#{class_name}",
				Record.create().set("grade_id", classes.getGrade_id()).set("class_name", classes.getClass_name()));
		if(count>0){
			renderJson(error("班级已经存在!"));
		}
		boolean temp = service.update(classes);
		if (temp) {
			renderJson(success(UPDATE_SUCCESS_MSG));
		} else {
			renderJson(error(UPDATE_FAIL_MSG));
		}
	}

	@Before(Trans.class)
	public void remove(){
		String ids = getPara("ids");
		int cnt = service.delWithDept(ids);
		if (cnt > 0) {
			renderJson(success(DEL_SUCCESS_MSG));
		} else {
			renderJson(error(DEL_FAIL_MSG));
		}
	}

	public void toImport(){
        setAttr("code", CODE);
        render(BASE_PATH + "classes_import.html");
    }

    public void doImport(){
		List<UploadFile> flist = this.getFiles("/temp", 1024*1024*50);
		if(flist.size()>0){
			UploadFile uf=flist.get(0);
			String result = service.doImport(uf.getFile());
			uf.getFile().delete();
			renderJson(success(result));
		}
    }

    public void chooseHeadTeacher(){
        int id = this.getParaToInt("id",0);
        int teacherId = this.getParaToInt("teacherId",0);
        boolean temp = service.chooseHeadTeacher(id,teacherId);
        if (temp) {
            renderJson(success(UPDATE_SUCCESS_MSG));
        } else {
            renderJson(error(UPDATE_FAIL_MSG));
        }
    }

    public void chooseSubjectTeacher(){
        int id = this.getParaToInt("id",0);
        String teachers = this.getPara("teachers");
        boolean temp = service.chooseSubjectTeacher(id,teachers);
        if (temp) {
            renderJson(success(UPDATE_SUCCESS_MSG));
        } else {
            renderJson(error(UPDATE_FAIL_MSG));
        }
    }

    public void upgrade(){
        boolean temp = service.newYear();
        if (temp) {
            renderJson(success(UPDATE_SUCCESS_MSG));
        } else {
            renderJson(error(UPDATE_FAIL_MSG));
        }
    }

    public void createGroupChat(){
        WeixinApi.createClassChat(getParaToInt("id"));
        renderJson(success(UPDATE_SUCCESS_MSG));
    }


}
